<?php

class UserController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
         $this->view->form = new App_Forms_UserForm("new");
          if($this->getRequest()->isGet())
          {
            $journeyId = $this->_getParam("id");    
            //Check the existance and avaliability
             if( $journeyId && JourneyTable::getInstance()-> isAvailableJourneyById($journeyId))
             {  
                //Save journey id in sessiom
                $sess = new Zend_Session_Namespace('JourneySub');
                $sess->journeyId= $journeyId;
             }
             else
             {
                $this->view->error = "This journey doesn't exist.";
             }             
          }
          else //POST Request
          {
              if ($this->view->form->isValid($_POST)) 
              {
                  //Check the uniqueness
                  if(!CustomerTable::getInstance()-> isNameExists($_POST['name']))
                  {
                     //Setting the new Customer
                     $user = new Customer();
                     $user->fromArray($_POST);
                    
                     //Get the journey id from session
                     $sess = new Zend_Session_Namespace('JourneySub');
                     $journeyId = $sess->journeyId;
                     unset($sess->journeyId);
                     if($journeyId)
                     {
                        //Save user
                        $user->save();
                        //Save subscription
                         $jrniSub = new JourneySubscribers();
                         $jrniSub->set('journey_id',$journeyId);
                         $jrniSub->set('customer_id', $user->get('id'));
                         $jrniSub->save();
                         //Save journey_subscription id in session
                         $sess = new Zend_Session_Namespace('JourneySub');
                         $sess->jrniSubId= $jrniSub->get("id");
                         $this->_redirect("user/success");
                     }
                     else
                     {
                         $this->view->error = "Please refresh page and re-submit.";
                     }
                  }
                  else
                  {
                        $this->view->error = "Sorry this name have been registered before.";
                  }
              }
         }
    }
   
    public function loginAction()
    {
         $this->view->form = new App_Forms_UserForm("login");  
          if($this->getRequest()->isGet())
          {
                $journeyId = $this->_getParam("id");    
                //Check the existance and avaliability
                 if( $journeyId && JourneyTable::getInstance()-> isAvailableJourneyById($journeyId))
                 {  
                    //Save journey id in sessiom
                    $sess = new Zend_Session_Namespace('JourneySub');
                    $sess->journeyId= $journeyId;
                 }
                 else
                 {
                    $this->view->error = "This journey doesn't exist.";
                 }             
          }
          else //POST Request
          {
             if ( $this->view->form->isValid($_POST)) 
             {      
                 //Authorization
                 $custId = CustomerTable::getInstance()-> getCustomer($_POST['name'],$_POST['telephone1'])->get("id");
                 if($custId)
                 {
                         //Get the journey id from session
                         $sess = new Zend_Session_Namespace('JourneySub');
                         $journeyId = $sess->journeyId;
                         unset($sess->journeyId);
                         if($journeyId)
                         {
                             //Checking the uniqueness of the 
                             if(!JourneySubscribersTable::getInstance()->isSubscribedBefore($journeyId,$custId))
                             {
                                 //Save subscription
                                 $jrniSub = new JourneySubscribers();
                                 $jrniSub->set('journey_id',$journeyId);
                                 $jrniSub->set('customer_id', $custId);
                                 $jrniSub->save();
                                 
                                 //Save journey_subscription id in session
                                 $sess = new Zend_Session_Namespace('JourneySub');
                                 $sess->jrniSubId= $jrniSub->get("id");
                                 $this->_redirect("user/success");
                             }
                             else
                             {
                                $this->view->error = "You have subscribed before for this journey.";
                             }
                         }
                         else
                         {
                             $this->view->error = "Please refresh page and re-submit.";
                         }
                 }
                else
                {
                    $this->view->error = "Invalid name or telephone1.";
                }
            }    
                           echo $this->view->error ;
                           exit;
         }
   }
    
    public function successAction()
    {
       if($this->getRequest()->isGet())
       {
          //Get the journey_subscription id from session
          $sess = new Zend_Session_Namespace('JourneySub');
          $jrniSubId = $sess->jrniSubId;
          if(!$jrniSubId)
          {
             Zend_Session::destroy(true);
             $this->_redirect("index");        
          }
       }
       else //POST Request
       {
          if($_POST['addComp'])
          {
             Zend_Session::destroy(true);
             $this->_redirect("index");        
          }
          else //add Companions	
          {
                 //Save number of companions in session
                 $sess = new Zend_Session_Namespace('JourneySub');
                 print_r($_POST);
                 $sess->numOfComp= $_POST['noOfComp'];
                 $this->_redirect("user/companion");
          }
       }
    }
    
    public function companionAction()
    {
          //Get the journey_subscription id from session
          $sess = new Zend_Session_Namespace('JourneySub');
          $this->view->numOfComp = $sess->numOfComp;
          if($this->view->numOfComp)
          {
             $this->view->forms = Array();
             for($i=0 ; $i <$this->view->numOfComp ; $i++)
             {
                    $this->view->forms[$i] = new App_Forms_UserForm("follower");   
                    $this->view->forms[$i]->setAttrib('id', 'follower_'.$i);  
             }
             if($this->getRequest()->isPost())
             {
                    //Get the journey_subscription id from session
                    $sess = new Zend_Session_Namespace('JourneySub');
                    $jrniSubId = $sess->jrniSubId;  
                    //Validation
                    $isAllValid = true;
                    $companions = new Doctrine_Collection('Followers'); 
                    $formsArr = explode ("_" ,$_POST["formsData"]);
                    for($i=0 ; $i <$this->view->numOfComp ; $i++)
                    {
                       $formData = explode ("&" ,trim($formsArr[$i]));
                       $formData = array('name'=>trim($formData[0]),'age'=>trim($formData[1]));
                       if(!$this->view->forms[$i]->isValid($formData))
                       {
                           $isAllValid = false;
                       }
                       else
                       {
                           //New follower
                           $companions[$i]->fromArray($formData);
                           $companions[$i]->set('journey_subscribers_id',$jrniSubId);
                       }
                    }
                    if($isAllValid)
                    {
                        //Save Collection
                        $companions->save();
                        //Remove All Session   
                        Zend_Session::destroy(true);
                        echo "You have added your companions successfully";
                        exit;
                   }     
             } 
          }
          else
          {
             Zend_Session::destroy(true);
             $this->_redirect("index");        
          }     
    }
}

